package com.ocom.accesservice.controller;

import com.alibaba.fastjson.JSON;
import com.ocom.accesservice.service.*;
import com.ocom.common.constant.AccessServiceUrl;
import com.ocom.common.entity.access.EntityMjAreaInfo;
import com.ocom.common.entity.access.EntityMjDeviceInfo;
import com.ocom.common.entity.access.EntityMjGroupPer;
import com.ocom.common.entity.access.EntityMjWhiteInfo;
import com.ocom.common.entity.master.EntityDeviceLogin;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
public class MachListController {


    @Autowired
    private GroupPerService groupPerService;

    @Autowired
    private DeviceInfoService deviceInfoService;

    @Autowired
    private AreaInfoService areaInfoService;

    @Autowired
    private RegionInfoService regionInfoService;

    /**
     * 根据用户查询设备列表（searchMachListByUser）
     * */
    @GetMapping(AccessServiceUrl.URL_SearchMachListByUser)
    public Result SearchMachListByUser(@RequestParam("comId") Long comId,@RequestParam("userId") Long userId){
        if (comId == null || userId == null){
            return ResultUtil.error(ResultEnum.Err_203);
        }
        ArrayList<Map<String,Object>> list = new ArrayList<>();
        // 从mj_group_per表查找com_id=companyId、p_id=userId、group_no>0的记录，获取area_id（数组）。
        List<EntityMjGroupPer> groupPerInfo = groupPerService.getGroupPerInfo(comId, userId);
        if (groupPerInfo.size() != 0){
            List<Long> areaId = groupPerInfo.stream().map(EntityMjGroupPer::getAreaId).collect(Collectors.toList());
            Long commMode = 1L;
            //从mj_device_info表查找这些area_id对应的（comm_mode=1,2）设备信息并返回。按region_id,area_id,device_no正向排序
            List<EntityMjDeviceInfo> deviceInfos = deviceInfoService.getDeviceInfo(comId, areaId, commMode);
            for (EntityMjDeviceInfo deviceInfo:deviceInfos) {
                Map<String, Object> map = new HashMap<>();
                map.put("deviceNo",deviceInfo.getDeviceNo());
                map.put("deviceName",deviceInfo.getDeviceName());
                map.put("areaId",deviceInfo.getAreaId());
                EntityMjAreaInfo areaInfo = areaInfoService.getAreaInfoById(comId, deviceInfo.getAreaId());
                map.put("areaName",areaInfo.getAreaName());
                map.put("regionId",areaInfo.getRegionId());
                map.put("regionName",regionInfoService.GetRegionName(areaInfo.getRegionId(), comId));
                map.put("commMode",deviceInfo.getCommMode());
                map.put("status",deviceInfo.getStatus());
                list.add(map);
            }

        }
        return ResultUtil.success(list);
    }

    // 重置门禁下传状态 (/mach/reset)
    @PostMapping(AccessServiceUrl.URL_MachReset)
    public Result MachReset(Long comId, String machNo){
        log.info("重置门禁下传状态:comId：{{}},machNo{{}}",comId,machNo);
        try {
            deviceInfoService.MachReset(comId,machNo);
        }catch (Exception exception){
            log.error("重置门禁下传状态失败{{}}",exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"重置门禁下传状态失败");
        }
        return ResultUtil.success();
    }


}
